package com.acvoli.shopping;

import com.acvoli.shopping.dto.CommitOrderForm;
import com.acvoli.shopping.dto.CommitOrderResponse;
import com.acvoli.shopping.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/api/v1/order/normal")
    public CommitOrderResponse commitOrder(@RequestBody CommitOrderForm form) {
        System.out.println("member id: " + form.getMemberId());
        System.out.println("sku id: " + form.getSkuId());
        System.out.println("sum: " + form.getSum());

        orderService.commitNormalOrder(form.getMemberId(), form.getSkuId(), form.getSum());
        return new CommitOrderResponse();
    }

    @Autowired
    private KafkaTemplate<Integer, String> kafkaTemplate;

    @PostMapping("/api/v1/order/kafka")
    public CommitOrderResponse commitOrderByKafka(@RequestBody CommitOrderForm form) {
        System.out.println("member id: " + form.getMemberId());
        System.out.println("sku id: " + form.getSkuId());
        System.out.println("sum: " + form.getSum());

        kafkaTemplate.send("test", form.getMemberId()
            + "_"
            + form.getSkuId()
            + "_"
            + form.getSum());

        return new CommitOrderResponse();
    }
}
